package com.sxz.sbs.module.client;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.sxz.sbs.R;
import com.sxz.sbs.app.AppConstant;
import com.sxz.sbs.applibrary.state.StateLayout;
import com.sxz.sbs.base.BaseActivity;
import com.sxz.sbs.http.retrofit.HttpParamMap;
import com.sxz.sbs.module.client.adapter.DealScheduleAdapter;
import com.sxz.sbs.module.client.bean.DealRecordBean;
import com.sxz.sbs.module.client.bean.DealScheduleBean;
import com.sxz.sbs.module.client.presenter.DetailsPlanPresenter;
import com.sxz.sbs.module.client.view.DetailsPlanView;
import com.sxz.sbs.widget.CommonTitle;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

/**
 * @author hp
 * @date 2018/1/12
 * 交易进度详情
 */

public class DealScheduleActivity extends BaseActivity<DetailsPlanPresenter> implements DetailsPlanView {

    @BindView(R.id.tv_stall_num)
    TextView tvStallNum;
    @BindView(R.id.tv_plot)
    TextView tvPlot;
    @BindView(R.id.tv_stall_adress)
    TextView tvStallAdress;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.stateLayout)
    StateLayout stateLayout;
    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;

    private DealScheduleAdapter dealScheduleAdapter;
    private DealRecordBean.ListBean intentBean;
    private int customId;
    private List<DealScheduleBean> dataList = new ArrayList<>();
    @Override
    protected void init() {
        new CommonTitle.Builder(this).setTitle("交易详情").build().show();
        intentBean = getIntent().getParcelableExtra("deal_record_detail");
        customId = getIntent().getIntExtra("customId", 0);
        if (intentBean != null) {
            tvStallNum.setText(intentBean.getNumber());
            tvPlot.setText(intentBean.getParkingName());
            tvStallAdress.setText(intentBean.getAddress());
        }
        refreshLayout.setColorSchemeResources(R.color.color_main);
        refreshLayout.setRefreshing(true);
        getDetailsPlanData();
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getDetailsPlanData();
            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        dealScheduleAdapter = new DealScheduleAdapter(dataList);
        recyclerView.setAdapter(dealScheduleAdapter);
        stateLayout.setOnRetryListener(new StateLayout.OnRetryListener() {
            @Override
            public void onRetry(int state) {
                getDetailsPlanData();
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_dealschedule;
    }

    @Override
    protected DetailsPlanPresenter createPresenter() {
        return new DetailsPlanPresenter();
    }


    @Override
    public void onDetailsPlanSuccess(List<DealScheduleBean> data) {
        if (mIsLive) {
            refreshLayout.setRefreshing(false);
            if (data != null && data.size() > 0) {
                stateLayout.showContent();
                dataList.clear();
                dataList.addAll(data);
                dealScheduleAdapter.notifyDataSetChanged();
            } else {
                stateLayout.showEmpty();
            }
        }

    }

    private void getDetailsPlanData() {
        /**
         customId	int	是	客户Id
         spaceId	int	是	车位Id
         */
        HashMap<String, Object> map = HttpParamMap.getParamMap();
        map.put("customId", customId);
        map.put("spaceId", intentBean.getSpaceId());
        mPresenter.getDetailsPlan(map);
    }

    @Override
    public void loginAgain() {
        super.loginAgain();
        refreshLayout.setRefreshing(false);
    }
    @Override
    public void showError(int errorTag, String msg) {
        refreshLayout.setRefreshing(false);
            if (errorTag == AppConstant.NET_ERROR_CODE) {
                stateLayout.showNetwork();
            } else {
                stateLayout.showEmpty();
            }

    }
}
